// pages/news/index.js
var util = require('../../utils/util.js');
const app = getApp()
var page = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrls: [],
        dataSource:[],
        loading: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        page = 1;
    },

    //滚动到底部刷新
    onReachBottom: function () {
        this.getNews();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getNews();
        this.initBanner();
    },
    //获取企业信息
    getNews: function () {
        var that = this;
        var limit = 10;
        wx.request({
            url: app.globalData.hostUrl + 'handler/getNewslist',
            data: {
                page: page,
                limit: limit,
            },
            methods: 'GET',
            header: {
                "Content-Type": "json"
            },
            success: function (res) {
//                console.log(res);
                if (res.data.result) {
                    page++
                    that.setData({
                        dataSource: that.data.dataSource.concat(res.data.result)
                    });
                    if (res.data.result.length < limit) {
                        that.setData({
                            loading: false
                        })
                    }
                }
                else {
                    that.setData({
                        loading: false
                    })
                    return false
                }
            }
        })
    },
    //广告图片初始化
    initBanner: function () {
        var that = this;
        var requrl = app.globalData.hostUrl + 'common/banner';
        var imgbanner = wx.getStorageSync('indexBanner');
        //console.log(imgbanner.length);
        if (!app.expiratTime('indexBanner') || imgbanner.length == 0) {
            wx.request({
                url: requrl,
                data: { 'location': '101' },
                header: {
                    'grant-appid': app.globalData.appid
                },
                success: function (res) {
                    if (res.statusCode === 200) {
                        //console.log(res);
                        wx.setStorageSync('indexBanner', res.data.result);
                        that.setData({
                            imgUrls: res.data.result
                        })
                    }
                }
            })
        }
        else {
            that.setData({
                imgUrls: imgbanner
            })
        }
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})